package conf2.ui;

import conf2.model.Deck;
import conf2.model.EActor.Side;
import conf2.model.ETerritory;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainWindow extends JFrame
{

    private final MainPanel mainPanel;
    private final InfoPanel infoPanel;
    private JPanel temporaryPanel = null;
    private List<JComponent> specialCardWidgets = new LinkedList<JComponent>();

    public MainWindow()
    {
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        setResizable(false);
        setIconImage(Images.get(Images.Which.APPICON));

        setLayout(new BorderLayout());

        mainPanel = new MainPanel();
        add(mainPanel, BorderLayout.CENTER);

        infoPanel = new InfoPanel();
        Image subImg = Images.get(Images.Which.TABLE).getSubimage(0, 600, 800, 200);
        infoPanel.setBgImage(subImg);
        add(infoPanel, BorderLayout.SOUTH);

        pack();
    }

    public Dimension getMainPanelSize()
    {
        return mainPanel.getSize();
    }

    public void addSpecialCardWidget(SpecialCardWidget scw)
    {
        specialCardWidgets.add(scw);
        mainPanel.add(scw);
    }

    public void addSpecialCardMiniature(SpecialCardMiniature scm)
    {
        specialCardWidgets.add(scm);
        mainPanel.add(scm);
    }

    public void highlightRegions(List<ETerritory> validMoves)
    {
        mainPanel.highlightRegions(validMoves);
    }

    public void removeSpecialCardWidgets()
    {
        for (JComponent c : specialCardWidgets) {
            mainPanel.remove(c);
        }
    }

    /**
     * A hackish procedure that removes all Components of the subtype
     * GamePiece from the main game panel.
     */
    public void removePieces()
    {
        // Maybe we could just use mainPanel.removeAll(), but this might be
        // a safer bet.

        Component[] comps = mainPanel.getComponents();
        for (Component c : comps) {
            if (c instanceof GamePiece) {
                mainPanel.remove(c);
            }
        }
    }

    public void addPiecesFromCollection(PieceCollection coll)
    {
        for (GamePiece gp : coll) {
            mainPanel.add(gp);
        }
    }

    public BufferedImage getBackgroundImage()
    {
        return mainPanel.getBackgroundImage();
    }

    public void setDecks(Deck myDeck, Deck opponentDeck)
    {
        infoPanel.setMyDeck(myDeck);
        infoPanel.setOpponentDeck(opponentDeck);
    }

    public void showBoard(Side homeSide)
    {
        mainPanel.showBoard(homeSide);
    }

    public void hideBoard()
    {
        mainPanel.hideBoard();
    }

    @Override
    public void validate()
    {
        super.validate();
        infoPanel.validate();
    }

    public void setTemporaryPanel(JPanel p)
    {
        temporaryPanel = p;
        remove(mainPanel);
        add(p, BorderLayout.CENTER);
        pack();
        repaint();
    }

    public void restorePanel()
    {
        if (temporaryPanel != null) {
            remove(temporaryPanel);
            add(mainPanel, BorderLayout.CENTER);
            pack();
            temporaryPanel = null;
            repaint();
        }
    }

    public void showCenteredWaitMessage(String text)
    {
        mainPanel.setCenterMessageVisible(false);
        mainPanel.setCenterMessage(text);
        mainPanel.setCenterMessageVisible(true);
    }

    public void hideCenteredWaitMessage()
    {
        mainPanel.setCenterMessageVisible(false);
    }
}
